package com.tomehay.termprojectmanage_server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tomehay.termprojectmanage_server.enums.ResponseCode;
import com.tomehay.termprojectmanage_server.mapper.GroupMemberMapper;
import com.tomehay.termprojectmanage_server.mapper.StudentMapper;
import com.tomehay.termprojectmanage_server.pojo.Model.GroupMember;
import com.tomehay.termprojectmanage_server.pojo.Model.Student;
import com.tomehay.termprojectmanage_server.pojo.Model.StudentGroup;
import com.tomehay.termprojectmanage_server.mapper.StudentGroupMapper;
import com.tomehay.termprojectmanage_server.pojo.VO.GroupMembersVO;
import com.tomehay.termprojectmanage_server.pojo.VO.ResultVO;
import com.tomehay.termprojectmanage_server.service.StudentGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tomehay
 * @since 2023-06-20
 */
@Service
public class StudentGroupServiceImpl extends ServiceImpl<StudentGroupMapper, StudentGroup> implements StudentGroupService {

    @Resource
    StudentMapper studentMapper;

    @Resource
    StudentGroupMapper studentGroupMapper;

    @Resource
    GroupMemberMapper groupMemberMapper;


    @Override
    public ResultVO<List<GroupMembersVO>> GetGroupsByCourseId(int courseId,int batchId) {
        List<GroupMembersVO> groupMembersVOS = new ArrayList<>();

        List<GroupMember> groupMembers;

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id",courseId);

        if (batchId != -52){
            queryWrapper.eq("batch_id",batchId);
        }

        List<StudentGroup> studentGroups = studentGroupMapper.selectList(queryWrapper);

        for (int i = 0; i < studentGroups.size(); i++) {
            GroupMembersVO groupMembersVO = new GroupMembersVO();
            BeanUtil.copyProperties(studentGroups.get(i),groupMembersVO,true);
            queryWrapper.clear();

            groupMembersVO.setGroupMemberList(studentGroupMapper.VOList(groupMembersVO.getId()));

            for (int j = 0; j < groupMembersVO.getGroupMemberList().size(); j++) {
//                groupMembersVO.getGroupMemberList().get(j).setName(Student.GetNoAndName(studentMapper,groupMembersVO.getGroupMemberList().get(j).getStudentId()));
                if (groupMembersVO.getGroupMemberList().get(j).getIsLeader()==1){
                    groupMembersVO.setGroupLeader(groupMembersVO.getGroupMemberList().get(j));
                    groupMembersVO.getGroupMemberList().remove(j);
                }
            }

            groupMembersVOS.add(groupMembersVO);
        }


        return new ResultVO<>(ResponseCode.auto(groupMembersVOS),groupMembersVOS);
    }

    @Override
    public ResultVO<int[]> GetBatches() {
        int[] batches = studentGroupMapper.GetBatches();
        return new ResultVO<>(ResponseCode.auto(batches),batches);
    }

    @Override
    public ResultVO<List<Student>> SelectUnGrouped(int courseId,int batchId) {
        List<Student> studentList = studentMapper.GetUnGroupedStudent(courseId,batchId);
        return new ResultVO<>(ResponseCode.auto(studentList),studentList);
    }

    @Override
    public ResultVO<Void> CreateStudentGroup(int[] students, int batchId, int course_id, int leader_id) {
        StudentGroup studentGroup = new StudentGroup();

        if (batchId == -1){
            batchId = studentGroupMapper.findMaxBatch_id()+1;
        }

        int No = studentGroupMapper.findMaxGroupNo(course_id,batchId);

        if (No==-1){
            studentGroup.setGroupNo(String.valueOf(batchId*1000+1));
        }
        else {
            studentGroup.setGroupNo(String.valueOf(No+1));
        }

        studentGroup.setCreateTime(LocalDateTime.now());
        studentGroup.setBatchId(batchId);
        studentGroup.setCourseId(course_id);

        studentGroupMapper.insert(studentGroup);


        for (int i = 0; i < students.length; i++) {
            GroupMember groupMember = new GroupMember();
            groupMember.setCreateTime(LocalDateTime.now());
            groupMember.setStudentId(students[i]);
            groupMember.setGroupId(studentGroup.getId());
            if (students[i] == leader_id) {
                groupMember.setIsLeader(1);
            }
            else {
                groupMember.setIsLeader(0);
            }
            groupMemberMapper.insert(groupMember);
        }

        return new ResultVO<>(ResponseCode.OK);
    }

    @Override
    public ResultVO<Void> DeleteStudentGroup(int groupId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",groupId);
        studentGroupMapper.delete(queryWrapper);
        queryWrapper.clear();
        queryWrapper.eq("group_id",groupId);
        groupMemberMapper.delete(queryWrapper);
        return new ResultVO<>(ResponseCode.OK);
    }

    @Override
    public ResultVO<GroupMembersVO> GetGroupsById(int id) {


        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);

        StudentGroup studentGroups = studentGroupMapper.selectOne(queryWrapper);

        GroupMembersVO groupMembersVO = new GroupMembersVO();
        BeanUtil.copyProperties(studentGroups,groupMembersVO,true);
        queryWrapper.clear();

        groupMembersVO.setGroupMemberList(studentGroupMapper.VOList(groupMembersVO.getId()));

        for (int j = 0; j < groupMembersVO.getGroupMemberList().size(); j++) {
            if (groupMembersVO.getGroupMemberList().get(j).getIsLeader()==1){
                groupMembersVO.setGroupLeader(groupMembersVO.getGroupMemberList().get(j));
                groupMembersVO.getGroupMemberList().remove(j);
            }
        }

        System.out.println(groupMembersVO);


        return new ResultVO<>(ResponseCode.auto(groupMembersVO),groupMembersVO);
    }
}
